-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hlb = require('libhlb')
printf = require "mod_print"

function device_open()
	local ret = hlb.usb_open(0x6666F0F2)
	if ret < 0 then
		print("Fail to open usb device")
		return false
	end
	
	return true
end

function device_close()
	local ret = hlb.usb_close()
	if ret < 0 then
		print("Fail to close usb device")
		return false
	end
	
	return true
end

function pause()
    io.write("\r\nAny key to continue ...")
    io.flush()
    io.read()
    print("")
end
-- -----------------------------------------------
function send_ctrl_cmd(cmd_data)
    if type(cmd_data) ~= "table" then
        print("cmd_data must be table type")
        return false
    end

    local msg = {0x40, 0xb0, 0, 0, #cmd_data, 100}
    local ret

    if #cmd_data > 0 then
        ret = hlb.usb_control_msg(cmd_data, msg)
    else
        ret = hlb.usb_control_msg(msg)
    end
    
    if ret < 0 then
        print("send_ctrl_cmd() ret = " .. ret)
        return false
    end

    return true
end
-- -----------------------------------------------
ret = device_open()
if ret == false then
    print("device_open fail")
    return
end

-- bulk write test
data = {0x00, 0x00}
ret = hlb.usb_bulk_write(0x01, data, 1000)
print("ret types: " .. type(ret))
if type(ret) == "number" then
    print(" ret = " .. ret)
end

-- bulk read test
ret = hlb.usb_bulk_read(0x81, 0x04, 1000)
print("ret types: " .. type(ret))
if type(ret) == "number" then
    print(" ret = " .. ret)
end

printf.table(ret, true)

--------------------------------------------------------------------------------
-- bulk write test
data = {0x01, 32, 0x00, 0x00, 0x00}
ret = hlb.usb_bulk_write(0x01, data, 1000)
print("ret types: " .. type(ret))
if type(ret) == "number" then
    print(" ret = " .. ret)
end

-- bulk read test
ret = hlb.usb_bulk_read(0x81, 37, 1000)
print("ret types: " .. type(ret))
if type(ret) == "number" then
    print(" ret = " .. ret)
end

printf.table(ret, true)
--------------------------------------------------------------------------------

--[[
pause()

--]]
device_close()
